// Terminated.java
package com.stable.actor.core;

import java.io.Serializable;
import java.time.Instant;

/**
 * 监控终止消息
 * 当被监控的 Actor 终止时，会向监控者发送此消息
 * 类似 Akka 的 Terminated 消息
 */
public class Terminated implements Serializable {

    private static final long serialVersionUID = 1L;

    private final ActorRef actor;
    private final boolean existenceConfirmed;
    private final boolean addressTerminated;
    private final Instant timestamp;

    public Terminated(ActorRef actor, boolean existenceConfirmed, boolean addressTerminated) {
        this.actor = actor;
        this.existenceConfirmed = existenceConfirmed;
        this.addressTerminated = addressTerminated;
        this.timestamp = Instant.now();
    }

    public Terminated(ActorRef actor) {
        this(actor, true, false);
    }

    /**
     * 获取终止的 Actor 引用
     */
    public ActorRef getActor() {
        return actor;
    }

    /**
     * 检查是否确认 Actor 存在
     */
    public boolean isExistenceConfirmed() {
        return existenceConfirmed;
    }

    /**
     * 检查是否是地址终止（整个 ActorSystem 关闭）
     */
    public boolean isAddressTerminated() {
        return addressTerminated;
    }

    /**
     * 获取终止时间戳
     */
    public Instant getTimestamp() {
        return timestamp;
    }

    @Override
    public String toString() {
        return String.format("Terminated(actor=%s, existenceConfirmed=%s, addressTerminated=%s, timestamp=%s)",
                actor, existenceConfirmed, addressTerminated, timestamp);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Terminated that = (Terminated) o;

        if (existenceConfirmed != that.existenceConfirmed) return false;
        if (addressTerminated != that.addressTerminated) return false;
        return actor != null ? actor.equals(that.actor) : that.actor == null;
    }

    @Override
    public int hashCode() {
        int result = actor != null ? actor.hashCode() : 0;
        result = 31 * result + (existenceConfirmed ? 1 : 0);
        result = 31 * result + (addressTerminated ? 1 : 0);
        return result;
    }
}
